package com.xindong.paking.controller;

import com.xindong.paking.entity.TbUser;
import com.xindong.paking.entity.vo.TbUserVo;
import com.xindong.paking.service.TbUserService;
import com.xindong.paking.utils.Result;
import com.xindong.paking.utils.ResultStatus;
import com.xindong.paking.utils.UserInfoUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.DataType;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wenbo
 * @since 2023-01-06
 */
@Api(tags = "用户模块")
@RestController
@RequestMapping("/user")
public class TbUserController {

    @Autowired
    private TbUserService tbUserService;

    @ApiOperation("新增用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名"),
            @ApiImplicitParam(name = "phone", value = "电话"),
            @ApiImplicitParam(name = "password", value = "密码"),
            @ApiImplicitParam(name = "realname", value = "真名"),
            @ApiImplicitParam(name = "gender", value = "性别（男：male，女：female）"),
            @ApiImplicitParam(name = "birth", value = "出生日期(2020-1-1 10:20:59)"),
            @ApiImplicitParam(name = "email", value = "邮箱")
    })
    @PostMapping("/add")
    public Result add(@RequestBody TbUserVo tbUserVo) {
        TbUser tbUser = new TbUser();
        BeanUtils.copyProperties(tbUserVo, tbUser);
        return tbUserService.addUser(tbUser);
    }

    @ApiOperation("分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页"),
            @ApiImplicitParam(name = "pageSize", value = "每页大小")
    })
    @GetMapping("list")
    public Result list(@RequestParam("page") Integer page, @RequestParam("pageSize") Integer pageSize) {
        return tbUserService.list(page, pageSize);
    }

    @ApiOperation("用户更新")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名"),
            @ApiImplicitParam(name = "phone", value = "电话"),
            @ApiImplicitParam(name = "password", value = "密码"),
            @ApiImplicitParam(name = "realname", value = "真名"),
            @ApiImplicitParam(name = "gender", value = "性别（男：male，女：female）"),
            @ApiImplicitParam(name = "birth", value = "出生日期(2020-1-1 10:20:59)"),
            @ApiImplicitParam(name = "email", value = "邮箱"),
            @ApiImplicitParam(name = "status", value = "账户状态（0：禁用，1：正常）"),
            @ApiImplicitParam(name = "role", value = "权限(0:普通用户,1:管理员)")
    })
    @PutMapping("update")
    public Result update(@RequestBody TbUser tbUser) {
        return tbUserService.updateUser(tbUser);
    }

    @ApiOperation("删除用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id")
    })
    @DeleteMapping("delete")
    public Result delete(@RequestParam("id") Integer id) {
        System.out.println("尝试还原分支");
        return tbUserService.deleteUser(id);
    }

    @ApiOperation("根据id查询用户")
    @GetMapping("selectById")
    public Result selectById(@RequestParam("id") Integer id) {
        return tbUserService.selectById(id);
    }
}

